import scrapy

from scrapy_base.items import EnterpirseItem, ScrapyBaseItem


class QichamaoSpider(scrapy.Spider):
    name = "qichamao"
    allowed_domains = ["qichamao.com"]
    start_urls = ["https://www.qichamao.com/representative/top.html"]

    def parse(self, response):
        lis = response.xpath("//div[@class='box-s person-box bg-f f14']/ul/li/a")

        for item in lis:
            href = item.attrib['href']
            detail = "https://www.qichamao.com/{}".format(href)

            yield scrapy.Request(url=detail, callback=self.detail_parse)


    # 人员名录 姓氏
    def detail_parse(self, response):
        for page in range(1001, 2000):
            next_page_url = response.request.url.replace(".html", "/p-{}.html".format(page))
            
            yield scrapy.Request(url=next_page_url, callback=self.next_page_detail_parse)


        # 人员名录
    def next_page_detail_parse(self, response):
        list = response.xpath("//div[@class='person-list person-name-list f-cb']/a")
        if list is not None:
            for item in list:
                it = ScrapyBaseItem()
                it['name'] = item.css("::text").extract_first()

                yield it

                enterpirse_url = item.xpath("@href").extract_first()
                yield scrapy.Request(url="https://www.qichamao.com/{}".format(enterpirse_url), callback=self.enterpirse_parse)

    def enterpirse_parse(self, response):
        table_tr = response.xpath("//table[@class='table-company box-s mt20']/tbody/tr")
        if table_tr is not None:
            for tr in table_tr:
                tds = tr.xpath("td")
                if tds is not None:
                    it = EnterpirseItem()
                    it['name'] = tds[0].css("::text").extract_first()
                    it['legal_representative'] = tds[1].css("::text").extract_first()
                    it['establishment_date'] = tds[2].css("::text").extract_first()
                    it['registered_capital'] = tds[3].css("::text").extract_first()
                    it['operating_status'] = tds[4].css("::text").extract_first()
                    it['area_code'] = tds[5].css("::text").extract_first()
                    it['province'] = tds[6].css("::text").extract_first()
                    it['area'] = tds[7].css("::text").extract_first()
                    it['address'] = tds[8].css("::text").extract_first()
                    it['unified_social_credit_code'] = tds[9].css("::text").extract_first()
                    it['contact_phone'] = tds[10].css("::text").extract_first()

                    yield it
